package com.yeyks.commonReference.service.mattress.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.em.HotelStatus;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.MattressInfoParam;
import com.yeyks.common.result.HotelNameLikeResult;
import com.yeyks.common.result.MattressInfoResult;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.commonReference.service.mattress.MattressInfoService;
import com.yeyks.hotel.dal.domain.HotelInfo;
import com.yeyks.mattress.dal.domain.MattressInfo;
import com.yeyks.mattress.dal.dao.MattressInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 共享床垫信息表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-07-16
 */
@Service
@Slf4j
public class MattressInfoServiceImpl extends ServiceImpl<MattressInfoMapper, MattressInfo> implements MattressInfoService {

    @Autowired
    private HotelInfoService hotelInfoService;

    /**
     * 获取床垫列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<MattressInfoResult> myList(MattressInfoParam param) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("imeiId", param.getImeiId());
        map.put("hotelName", param.getHotelName());
        map.put("status", param.getStatus());
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MattressInfo> mattressInfos = baseMapper.myList(map);
        PageInfo<MattressInfo> of = PageInfo.of(mattressInfos);
        return new PageResult<>(of.getTotal(), of.getPageNum(), of.isHasNextPage(), of.getNextPage(), getMattressInfoResult(mattressInfos));
    }

    /**
     * 通过酒店名称模糊查询
     *
     * @param hotelName 酒店名称
     * @return
     */
    @Override
    public List<HotelNameLikeResult> hotelNameLike(String hotelName) {
        List<HotelInfo> hotelInfos = hotelInfoService.list(new QueryWrapper<HotelInfo>().like(HotelInfo.NAME, hotelName));
        if (null == hotelInfos || hotelInfos.size() == 0) {
            return Lists.newArrayList();
        }
        return ObjectConvertUtil.convertList(hotelInfos, HotelNameLikeResult.class);
    }

    /**
     * 绑定酒店与设备
     *
     * @param id        设备序号
     * @param hotelId   酒店id
     * @param loginUser 当前操作人
     * @return
     */
    @Override
    public Boolean bind(Integer id, Integer hotelId, AccountInfo loginUser) {
        MattressInfo mattressInfo = baseMapper.selectById(id);
        if (null == mattressInfo) {
            log.info("根据设备序号查询不到设备id = {}", id);
            throw new ServiceException("该设备不存在");
        }
        if (null != mattressInfo.getHotelId()) {
            throw new ServiceException("该设备已绑定其他酒店");
        }
        HotelInfo hotelInfo = hotelInfoService.getById(hotelId);
        if (null == hotelInfo) {
            log.info("绑定的酒店不存在hotelId = {}", hotelId);
            throw new ServiceException("绑定的酒店不存在");
        }
        if (null == hotelInfo.getStatus()) {
            log.info("酒店启用状态异常hotelId = {}", hotelId);
            throw new ServiceException("酒店启用状态异常");
        }
        if (HotelStatus.Pass != hotelInfo.getStatus()) {
            log.info("酒店启用状态异常hotelId = {}", hotelId);
            throw new ServiceException("非审核通过酒店不能绑定");
        }
        mattressInfo.setHotelId(hotelId);
        mattressInfo.setModifyTime(new Date());
        mattressInfo.setOperatorId(loginUser.getId());
        if (1 != baseMapper.updateById(mattressInfo)) {
            throw new ServiceException("绑定酒店与设备异常");
        }
        return true;
    }

    /**
     * 新增床垫
     *
     * @param imeiId    床垫编号
     * @param loginUser 当前操作人
     * @return
     */
    @Override
    public Boolean insert(String imeiId, AccountInfo loginUser) {
        MattressInfo mattressInfo = new MattressInfo();
        mattressInfo.setOperatorId(loginUser.getId());
        //新增的床垫为可用状态(1)
        mattressInfo.setStatus(1);
        mattressInfo.setImeiId(imeiId);
        //TODO 待确定
        mattressInfo.setDeviceId(imeiId);
        Date date = new Date();
        mattressInfo.setCreateTime(date);
        mattressInfo.setModifyTime(date);
        if (1 != baseMapper.insert(mattressInfo)) {
            throw new ServiceException("新增床垫异常");
        }
        return true;
    }

    /**
     * 床垫列表对象转换
     *
     * @param mattressInfos
     * @return
     */
    private List<MattressInfoResult> getMattressInfoResult(List<MattressInfo> mattressInfos) {
        List<MattressInfoResult> results = Lists.newArrayList();
        for (MattressInfo mattressInfo : mattressInfos) {
            MattressInfoResult result = ObjectConvertUtil.convertObject(mattressInfo, MattressInfoResult.class);
            if (null == result) {
                continue;
            }
            if (null != mattressInfo.getHotelId()) {
                HotelInfo hotelInfo = hotelInfoService.getById(mattressInfo.getHotelId());
                if (null != hotelInfo) {
                    result.setHotelName(hotelInfo.getName());
                    result.setHotelPhone(hotelInfo.getPhone());
                    result.setContactName(hotelInfo.getContactName());
                }
            }
            results.add(result);
        }
        return results;
    }
}
